﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using NHibernate;
using SimpleScrum.Areas.Scrum.Models;
using SimpleScrum.Core.Entities;
using SimpleScrum.Helpers;
using SimpleScrum.Utils;

namespace SimpleScrum.Areas.Scrum.Controllers
{
    public class ProjectController : Controller
    {
        public OrganizationModel ObjOrg;
        public ProjectModel ObjProject;
        private ISession _session = NHibernateHelper.GetSession();

        public ProjectController()
        {
            ObjProject = new ProjectModel();
            ObjOrg = new OrganizationModel();
        }

        // 
        // GET: /Project/ 
        [HttpGet]
        public ActionResult Index()
        {
            ViewBag.org = Client.Instance.CurrentOrganizationId;
            IList<Project> projects = ObjProject.GetProjects(Client.Instance.CurrentOrganizationId);
            return View(projects);
        }

        // 
        // GET: /Project/Details/
        [HttpGet]
        public ActionResult Details(Guid id)
        {
            Client.Instance.CurrentProjectId = id;
            Project project = ObjProject.GetProjectById(id);
            Client.Instance.CurrentOrganizationId = project.Organization.Id;
            return View(project);
        }

        // 
        // GET: /Project/Create 
        [HttpGet]
        public ActionResult New()
        {
            var project = new Project();
            return View(project);
            //return View(new Project{StatusName = Project.StatusNames.Continued});
        }

        // 
        // POST: /Project/Create 
        [HttpPost]
        public ActionResult New(Project project)
        {
            try
            {
                project.Organization = ObjOrg.GetOrganizationById(Client.Instance.CurrentOrganizationId);
                ObjProject.NewProject(project);
                Client.Instance.CurrentProjectId = project.Id;
                Client.Instance.CurrentOrganizationId = project.Organization.Id;
                if (Client.Instance.DefaultProjectId == Guid.Empty)
                {
                    //Client.Instance.DefaultProjectId = project.Id;
                    return RedirectToAction("Index", "Initialize");
                }
                else
                {
                    return RedirectToAction("Details", "Project", new {id = project.Id});
                }
            }
            catch
            {
                return View();
            }
        }

        // 
        // GET: /Project/Edit/5 
        [HttpGet]
        public ActionResult Edit(Guid id)
        {
            Project project = ObjProject.GetProjectById(id);
            Client.Instance.CurrentOrganizationId = project.Organization.Id;
            return View(project);
        }

        // 
        // POST: /Project/Edit/5 
        [HttpPost]
        public ActionResult Edit(Guid id, Project project)
        {
            try
            {
                project.Organization = ObjOrg.GetOrganizationById(Client.Instance.CurrentOrganizationId);
                ObjProject.UpdateProject(project);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // 
        // GET: /Project/Delete/5 
        [HttpGet]
        public ActionResult Delete(Guid id)
        {
            Project project = ObjProject.GetProjectById(id);
            return View(project);
        }

        // 
        // POST: /Project/Delete/5 
        [HttpPost]
        public ActionResult Delete(Guid id, FormCollection collection)
        {
            try
            {
                Project project = ObjProject.GetProjectById(id);
                project.Organization = ObjOrg.GetOrganizationById(Client.Instance.CurrentOrganizationId);
                ObjProject.DeleteProject(project);
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                return View();
            }
        }
    }
}